<template>
    <div id="container" :class="[smallWindow ? 'radius' : '']">
        <div v-if="smallWindow" class="smallwindow" style="-webkit-app-region: drag;" >
            <div class="timing-smallbox">
                <div class="small_bg first">
                    <div class="s-time-font">
                        {{minuteTens}}
                    </div>
                </div>
                <div class="small_bg second">
                    <div class="s-time-font">
                        {{minuteUnits}}
                    </div>
                </div>
                <div class="small_dot">
                    <img src="../../assets/img/small_dot.png" alt="">
                </div>
                <div class="small_bg third">
                    <div class="s-time-font">
                        {{secondTens}}
                    </div>
                </div>
                <div class="small_bg last">
                    <div class="s-time-font">
                        {{secondUnits}}
                    </div>
                </div>
            </div>
            <div class="exitSmall" style="z-index: 9999 !important;-webkit-app-region: no-drag;" @click="exitSmall">
                <img src="../../assets/img/exitsmallWindow.png"  alt="">
                <span style="margin-left:6px;position:relative;top:-1px" class="exitText">退出小窗</span>
            </div>
        </div>
        <div v-if="!smallWindow" class="title-wrap">
            <div class="title-text">
                <img src="../../assets/img/countdown_icon.png" alt="" />
                <span>活动计时</span>
            </div>
            <div class="title-btns">
                <img src="../../assets/img/mini-w.png" @click="windowMin" alt="" />
                <img src="../../assets/img/close-w.png" @click="windowClose" alt="" />
            </div>
        </div>
        <div v-if="!smallWindow" class="content">
            <div class="count-down-container" style="-webkit-app-region: no-drag;">
                <div class="time-action">
                    <div class="timing-box">
                        <div class="timing-minutes-action first">
                            <div class="timing-minutes">
                                <div class="time-font">
                                    {{minuteTens}}
                                </div>
                            </div>
                            <div class="timing-plus-add" v-if="changeTime">
                                <div class="add_plus">
                                    <div class="add" @click.prevent="addCount(true, true)">+</div>
                                    <div class="plus" @click.prevent="plusCount(true, true)">-</div>
                                </div>
                                <!-- <img class="pointer" src="../../assets/img/add.png"
                                    @click.prevent="addCount(true, true)" />
                                <img class="pointer" src="../../assets/img/plus.png"
                                    @click.prevent="plusCount(true, true)" /> -->
                            </div>
                        </div>
                        <div class="timing-minutes-action second">
                            <div class="timing-minutes">
                                <div class="time-font">
                                    {{minuteUnits}}
                                </div>
                            </div>
                            <div class="timing-plus-add" v-if="changeTime">
                                <div class="add_plus">
                                    <div class="add" @click.prevent="addCount(true, false)">+</div>
                                    <div class="plus" @click.prevent="plusCount(true, false)">-</div>
                                </div>
                            </div>
                        </div>
                        <span class="timing-minutes-colon">:</span>
                        <div class="timing-minutes-action third">
                            <div class="timing-minutes">
                                <div class="time-font">
                                    {{secondTens}}
                                </div>
                            </div>
                            <div class="timing-plus-add" v-if="changeTime">
                                <div class="add_plus">
                                    <div class="add" @click.prevent="addCount(false, true)">+</div>
                                    <div class="plus" @click.prevent="plusCount(false, true)">-</div>
                                </div>
                            </div>
                        </div>
                        <div class="timing-minutes-action last">
                            <div class="timing-minutes">
                                <div class="time-font">
                                    {{secondUnits}}
                                </div>
                            </div>
                            <div class="timing-plus-add" v-if="changeTime">
                                <div class="add_plus">
                                    <div class="add" @click.prevent="addCount(false, false)">+</div>
                                    <div class="plus" @click.prevent="plusCount(false, false)">-</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div v-if="!reStartOrContinue">
                    <el-button class="timing-button" :class="[!this.isStart ? 'start' : 'end']"
                        @click.prevent="startCount">{{countText}}</el-button>
                </div>
                <div v-if="reStartOrContinue">
                    <el-button class="timing-button restart" 
                        @click.prevent="restartCount">重新开始</el-button>
                    <el-button class="timing-button again" 
                        @click.prevent="continueCount">继续计时</el-button>
                </div>
            </div>
            <div class="right_content">
                <div class="right_btn" @click="windowSmall">
                    <img src="../../assets/img/mini_windom.png" alt="" style="position:absolute;left:20px;top:12px">
                    <div @click="windowSmall" >小窗模式</div>
                </div>
                <div class="right_btn" >
                    <img src="../../assets/img/bg_music.png" alt="" style="position:absolute;left:20px;top:12px">
                    <div style="z-index:999px" @click="open_bgMusic">背景音乐</div>
                    <img src="../../assets/img/music_down.png" alt="" @click="open_bgMusic" style="z-index:999px !important;position:absolute;right:20px;top:15px">
                </div>
            </div>
            <div v-if="bgmusic" class="bgMusic">
                <div class="bgMusic_select" @click="select_bgMusic(type = 1)"><div class="dot"><img v-if="music_type === '1'" class="blueDot" src="../../assets/img/bule_dot.png" alt=""></div> <span> 舒缓</span></div>
                <div class="bgMusic_select" @click="select_bgMusic(type = 2)"><div class="dot"><img v-if="music_type === '2'" class="blueDot" src="../../assets/img/bule_dot.png" alt=""></div> <span> 柔和</span></div>
                <div class="bgMusic_select" @click="select_bgMusic(type = 3)"><div class="dot"><img v-if="music_type === '3'" class="blueDot" src="../../assets/img/bule_dot.png" alt=""></div> <span> 昂扬</span></div>
                <div class="bgMusic_select" @click="select_bgMusic(type = 0)"><div class="dot"><img v-if="music_type === '0'" class="blueDot" src="../../assets/img/bule_dot.png" alt=""></div> <span> 静音</span></div>
            </div>
        </div>
        <audio id="countdown" preload="auto">
            <source src="../../assets/sound/countdown.mp3" type="audio/mpeg">
        </audio>
        <audio id="countdown-music1" preload="auto" loop="loop">
            <source src="../../assets/sound/music1.mp3" type="audio/mpeg">
        </audio>
        <audio id="countdown-music2" preload="auto" loop="loop">
            <source  src="../../assets/sound/music2.mp3" type="audio/mpeg">
        </audio>
        <audio id="countdown-music3" preload="auto" loop="loop">
            <source  src="../../assets/sound/music3.mp3" type="audio/mpeg">
        </audio>
    </div>
</template>

<script>
// import mixin from '../../mixin/mixin'

export default {
    // mixins: [mixin],
    name: "countDown",
    data () {
        return {
            minuteTens: 0,
            minuteUnits: 0,
            secondTens: 0,
            secondUnits: 0,
            isStart: false,
            reStartOrContinue: false,
            bgmusic:false, 
            music_type:'0',
            smallWindow:false,
            InEnd:false,
            changeTime:true,
        }
    },
    watch:{
        secondUnits(val){
            if (val === 3 && this.minuteTens === 0 && this.minuteUnits === 0 && this.secondTens === 0 && this.isStart) {
                this.InEnd = true
                document.getElementById('countdown').volume = 0.1
                document.getElementById('countdown').play();
            }
            if (val === 0 && this.minuteTens === 0 && this.minuteUnits === 0 && this.secondTens === 0 ) {
                this.changeTime = true
                this.InEnd = false
                console.log('时间到');
                this.handlePlayLoad()
            }
        },
    },
    mounted () {
        if (window.countDownTimeOut) {
            clearInterval(window.countDownTimeOut);
            window.countDownTimeOut = null;
        }
    },
    computed: {
        zeroTime () {
            return this.minuteTens === 0 && this.minuteUnits === 0 && this.secondTens === 0 && this.secondUnits === 0
        },
        countText () {
            return !this.isStart ? "开始计时" : '停止计时'
        },
    },
    methods: {
        open_bgMusic(){
            if (this.isStart === true ) {
                this.$message.warning('请先暂停计时后切换背景音乐')
                return
            }
            this.bgmusic = !this.bgmusic
        },
        select_bgMusic(type){
            switch (type) {
                case 1:
                    this.music_type = "1"
                    break;
                case 2:
                    this.music_type = "2"
                    break;
                case 3:
                    this.music_type = "3"
                    break;
                case 0:
                    this.music_type = "0"
                    break;
            }
        },
        handlePlay(){
            if (this.music_type === '1') {
                document.getElementById('countdown').volume = 0.4
                document.getElementById('countdown-music1').play();
            }else if (this.music_type === '2') {
                document.getElementById('countdown').volume = 0.4
                document.getElementById('countdown-music2').play();
            }else if (this.music_type === '3') {
                document.getElementById('countdown').volume = 0.4
                document.getElementById('countdown-music3').play();
            }
        },
        handlePlayPause(){
            if (this.music_type === '1') {
                document.getElementById('countdown-music1').pause();
            }else if (this.music_type === '2') {
                document.getElementById('countdown-music2').pause();
            }else if (this.music_type === '3') {
                document.getElementById('countdown-music3').pause();
            }
        },
        handlePlayLoad(){
            if (this.music_type === '1') {
                document.getElementById('countdown-music1').load();
            }else if (this.music_type === '2') {
                document.getElementById('countdown-music2').load();
            }else if (this.music_type === '3') {
                document.getElementById('countdown-music3').load();
            }
        },
        startCount () {
            if (!this.isStart) {
                if (this.minuteTens === 0 && this.minuteUnits === 0 && this.secondTens === 0 && this.secondUnits === 0) {
                    this.$message.warning("请先选择时间！")
                    return
                }
                this.setTimeInterval()
                this.handlePlay()
                this.isStart = true;
                this.bgmusic = false;
                this.changeTime = false
            } else {
                if (!this.zeroTime) {
                    clearInterval(window.countDownTimeOut)
                    this.reStartOrContinue = true
                    this.isStart = false
                    document.getElementById('countdown').pause()
                    this.handlePlayPause()
                }else{
                }
            }
        },
        continueCount () {
            this.setTimeInterval()
            this.isStart = true;
            this.reStartOrContinue = false
            this.bgmusic = false
            if (this.InEnd === true) {
                document.getElementById('countdown').play()
            }
            this.handlePlay()
        },
        restartCount () {
            this.secondUnits = this.secondTens = this.minuteUnits = this.minuteTens = 0
            // this.isStart = true;
            this.reStartOrContinue = false
            this.changeTime = true
            this.InEnd = false
            document.getElementById('countdown').load();
            this.handlePlayLoad()
        },
        setTimeInterval () {
            if (window.countDownTimeOut) {
                clearInterval(window.countDownTimeOut);
                window.countDownTimeOut = null;
            }
            let totalSeconds = (this.minuteTens * 10 + this.minuteUnits) * 60 + (this.secondTens * 10 + this.secondUnits);
            let _this = this;
            window.countDownTimeOut = setInterval(function () {
                if (totalSeconds === 0) {
                    clearInterval(window.countDownTimeOut);
                } else {
                    totalSeconds = totalSeconds - 1;
                }
                _this.countDown(totalSeconds);
            }, 1000);
        },
        addCount (isMinute, isTens) {
            if (this.isStart) {
                this.$message.warning("请先等待计时结束！");
                return;
            }
            if (isMinute) {
                if (isTens) {
                    if (this.minuteTens < 5) {
                        this.minuteTens = this.minuteTens + 1;
                    }
                } else {
                    if (this.minuteUnits < 9) {
                        this.minuteUnits = this.minuteUnits + 1;
                    }
                }
            } else {
                if (isTens) {
                    if (this.secondTens < 5) {
                        this.secondTens = this.secondTens + 1;
                    }
                } else {
                    if (this.secondUnits < 9) {
                        this.secondUnits = this.secondUnits + 1;
                    }
                }
            }
        },
        plusCount (isMinute, isTens) {
            if (this.isStart) {
                this.$message.warning("请先等待计时结束！");
                return;
            }
            if (isMinute) {
                if (isTens) {
                    if (this.minuteTens > 0) {
                        this.minuteTens = this.minuteTens - 1;
                    }
                } else {
                    if (this.minuteUnits > 0) {
                        this.minuteUnits = this.minuteUnits - 1;
                    }
                }
            } else {
                if (isTens) {
                    if (this.secondTens > 0) {
                        this.secondTens = this.secondTens - 1;
                    }
                } else {
                    if (this.secondUnits > 0) {
                        this.secondUnits = this.secondUnits - 1;
                    }
                }
            }
        },
        countDown (totalSeconds) {
            if (this.minuteTens === 0 && this.minuteUnits === 0 && this.secondTens === 0 && this.secondUnits === 0) {
                return;
            }
            if (totalSeconds === 2) {
                // document.getElementById('countdown-sound').play();
            }
            if (totalSeconds < 0) {
                return;
            }
            let minutes = parseInt(totalSeconds / 60);
            let seconds = totalSeconds % 60

            if (minutes >= 10) {
                minutes = minutes + "";
                this.minuteTens = parseInt(minutes.substring(0, 1));
                this.minuteUnits = parseInt(minutes.substring(1, 2));
            } else {
                minutes = minutes + "";
                this.minuteTens = 0;
                this.minuteUnits = parseInt(minutes)
            }

            if (seconds >= 10) {
                seconds = seconds + "";
                this.secondTens = parseInt(seconds.substring(0, 1));
                this.secondUnits = parseInt(seconds.substring(1, 2));
            } else {
                seconds = seconds + "";
                this.secondTens = 0;
                this.secondUnits = parseInt(seconds);
            }

            if (totalSeconds === 0) {
                this.isStart = false;
            }
        },

        windowMin() {
            this.$electron.ipcRenderer.send("resource-window-minimize", {
              metaId: "countDown",
            });
        },
        windowSmall(){
            this.$electron.ipcRenderer.send("resource-window-changemize", {
              metaId: "countDown",
            });
            this.smallWindow = true
        },
        exitSmall(){
            this.smallWindow = false;
            this.$electron.ipcRenderer.send("resource-window-normalmize", {
              metaId: "countDown",
            });
        },
        windowClose() {
            this.deleteArr = []
            this.$electron.ipcRenderer.send("close-resource-window", {
              metaId: "countDown",
            });
        },
    }
}
</script>

<style lang="scss">
.radius{
    // border-radius: 20px;
    overflow: hidden; 
    background-image:none !important;
    background-color: transparent;
    border: none;
}
#container {
    width: 100%;
    min-height: 100vh;
    background-image: url("../../assets/img/random_body.jpg");
    background-size: cover;
    .smallwindow{
        width: 100vw;
        height: 100vh;
        background-image: url("../../assets/img/countdownSmall.png");
        background-size: cover;
        position: relative;
        overflow: hidden;
        .timing-smallbox{
            width: 100%;
            display: flex;
            position: relative;
            .small_bg{
                width: 16vw;
                height: 47vh;
                background-image: url("../../assets/img/time_count_small.png");
                background-size: cover;
                border-radius: 6px;
                display: flex;
                justify-content: center;
                align-items: center;
                .s-time-font{
                    // width: 10vw;
                    // height: 29vh;
                    font-size: 68px;
                    font-weight: 400;
                    color: #303133;
                    // position: absolute;
                    // left: 2.5vw;
                    // top: -1vh;
                }
            }
            .small_dot{
                // width: 6px;
                // height: 23px;
                // background-image: url("../../assets/img/small_dot.png");
                // background-size: cover;
                position: absolute;
                top: 36vh;
                left: 49vw;
            }
            .first {
                position: absolute;
                top: 17vh;
                left: 10vw;
            }
            .second {
                position: absolute;
                top: 17vh;
                left: 30vw;
            }
            .third {
                position: absolute;
                top: 17vh;
                right: 30vw;
            }
            .last {
                position: absolute;
                top: 17vh;
                right:10vw;
            }
        }
        .exitSmall{
            cursor: pointer;
            width: 35vw;
            height: 17vh;
            background: #85B9EC;
            border-radius: 15px;
            position: absolute;
            left:32vw;
            bottom: 10vh;
            display: flex;
            justify-content: center;
            align-items: center;
            img{
                width: 14px;
                height: 15px;
            }
            .exitText{
                // width: 66px;
                // height: 16px;
                font-size: 16px;
                font-weight: 400;
                color: #FFFFFF;
            }
        }
    }
    .title-wrap {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 64px;
        background-image: url("../../assets/img/title_bg.png");
        background-size: cover;
        -webkit-app-region: drag;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 30px;
        box-sizing: border-box;
        z-index: 98;
   
       .title-text {
         display: flex;
         align-items: center;
            img{
                margin-right: 8px;
                width: 20px;
                height: 20px;
            }
            span {
                font-size: 18px;
                color: #fff;
            }
        }
   
       .title-btns {
         -webkit-app-region: no-drag;
            img {
                width: 14px;
                height: 14px;
                margin-left: 18px;
                cursor: pointer;
            }
        }
    }
     .content{
        display: flex;
        justify-content: center;
        height: 836px;
    }
    .count-down-container {
        width: 924px;
        height: 481px;
        background: url("../../assets/img/count-down.png");
        background-size: cover;
        position: absolute;
        top: 205px;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;

        .time-action {
            width: 900px;
            height: 360px;
            border-radius: 10px;
            .timing-box {
                width: 100%;
                position: relative;
                &::before {
                    content: "分";
                    position: absolute;
                    top: 60px;
                    left: 236px;
                    color: #909399 ;
                    font-size: 32px;
                }
                &::after {
                    content: "秒";
                    position: absolute;
                    top: 60px;
                    right: 238px;
                    color: #909399 ;
                    font-size: 32px;
                }

                .timing-minutes-action {
                    height: 222px;
                    width: 146px;
                    position: relative;
                    &.first {
                        position: absolute;
                        background: url("../../assets/img/time-count.png") no-repeat ;
                        background-size: cover; 
                        border-radius: 6px;
                        top: 118px;
                        left: 90px;
                    }
                    &.second {
                        position: absolute;
                        background: url("../../assets/img/time-count.png") no-repeat ;
                        background-size: cover; 
                        border-radius: 6px;
                        top: 118px;
                        left: 267px;
                    }
                    &.third {
                        position: absolute;
                        background: url("../../assets/img/time-count.png") no-repeat ;
                        background-size: cover; 
                        border-radius: 6px;
                        top: 118px;
                        right: 267px;
                    }
                    &.last {
                        position: absolute;
                        background: url("../../assets/img/time-count.png") no-repeat ;
                        background-size: cover; 
                        border-radius: 6px;
                        top: 118px;
                        right: 91px;
                    }

                    .timing-minutes {
                        font-size: 180px;
                        margin: 50px 20px;
                        position: relative;
                        top: 12px;
                    }

                    .timing-plus-add {
                        width: 147px;
                        height: 41px;
                        background: #fff;
                        border-radius: 50px;
                        position: relative;
                        top: 49px;
                        // margin: 29px auto 0px auto;
                        .add_plus{
                            display: flex;
                            justify-content: space-between;
                            padding: 0 26px;
                            cursor: pointer;
                        }
                        .add{
                            width: 22px;
                            height: 21px;
                            font-size: 43px;
                            font-weight: 400;
                            color: #909399;
                            position: relative;
                            top: -10px;
                        }
                        .plus{
                            width: 22px;
                            height: 21px;
                            font-size: 43px;
                            font-weight: 400;
                            color: #909399;
                            position: relative;
                            top: -10px;
                        }
                    }
                }

                .timing-minutes-colon {
                    position: absolute;
                    top: 204px;
                    left: 440px;
                    text-align: center;
                    font-weight: 600;
                    font-size: 73px;
                    line-height: 43px;
                    color: #303133 ;
                    float: left;
                }

                .time-font {
                    width: 61px;
                    height: 95px;
                    font-size: 180px;
                    font-weight: 400;
                    color: #303133 ;
                    line-height: 95px;
                }
            }
        }

        .el-button {
            span {
                font-size: 36px;
                color: rgba(255, 255, 255, 1);
            }
        }

        .timing-button {
            width: 329px;
            height: 80px;
            border-radius: 40px;
            position: relative;
            top: 142px;
            font-size: 32px;
        }
        .restart{
            background: #F4A323;
            position: relative;
            left:-120px;

        }
        .again{
            background: #4894FF;
            position: relative;
            left:120px;
        }
        .start{
            background: #4894FF;
        }
        .end{
            background: #F4A323;
        }
    }
    .right_content{
        position: absolute;
        right: 57px;
        top: 360px;
        .right_btn{
            cursor: pointer;
            position: relative;
            width: 165px;
            height: 40px;
            background: #85B9EC;
            border-radius: 20px;
            text-align: center;
            padding-top: 5px;
            margin: 30px 0;
            font-size: 18px;
            color: #FFFFFF;
            z-index: 999 !important;
        }
    }
    .bgMusic{
        padding-top: 40px;
        padding-left: 20px;
        z-index: inherit;
        position: absolute;
        top: 460px;
        right: 57px;
        width: 165px;
        height: 206px;
        color: #909399;
        font-size: 18px;
        background: #F2F2F2;
        // background: 
        // linear-gradient(transparent,  #F2F2F2);
        box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.15);
        border-radius: 20px;
        .bgMusic_select{
            cursor: pointer;
            height: 20px;
            line-height: 20px;
            margin: 15px;
            display: flex;
            .dot{
                width: 16px;
                height: 16px;
                border-radius: 50%;
                margin-right: 8px;
                border: 1px solid #909399;
                position: relative;
                top: 2px;
                .blueDot{
                    position: absolute;
                    top: 1.2px;
                    left: 1.2px;
                }
            }
        }
    }
}
</style>
